package com.store.pet.petstore.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.store.pet.petstore.bean.UserBean;
import com.store.pet.petstore.util.Constant;
import com.store.pet.petstore.util.SharedPreferencesUtil;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 基类Fragment
 */
public abstract class BaseFragment extends Fragment implements View.OnClickListener {

    protected View view;

    private Unbinder unbinder;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(getLayoutID(), container, false);
            unbinder = ButterKnife.bind(this, view);
            initView(savedInstanceState);
        }
        return view;
    }

    /**
     * 获取布局文件
     */
    protected abstract int getLayoutID();

    /**
     * 初始化数据
     *
     * @param savedInstanceState
     */
    protected abstract void initView(Bundle savedInstanceState);

    /**
     * 返回用户角色
     */
    protected int getUserRole(){
        if(SharedPreferencesUtil.getBeanFromSp(getContext(),Constant.FILE_NAME,Constant.DATA) != null){
            UserBean bean = SharedPreferencesUtil.getBeanFromSp(getContext(),Constant.FILE_NAME,Constant.DATA);
            return bean.getContent().getRole();
        }
        return 0;
    }

    /**
     * 返回用户
     */
    protected int getMyId(){
        if(SharedPreferencesUtil.getBeanFromSp(getContext(),Constant.FILE_NAME,Constant.DATA) != null){
            UserBean bean = SharedPreferencesUtil.getBeanFromSp(getContext(),Constant.FILE_NAME,Constant.DATA);
            return bean.getContent().getId();
        }
        return 0;
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }

    @Override
    public void onClick(View view) {

    }
}
